// src/pages/webIndex/curAddress/curAddress.js
import getIn from '../../../../yali-sdk/utils/getIn';
import getLocation from '../../../common/location';
import QQMapWX from '../../../../yali-sdk/lib/qqmap-wx-jssdk.min.js';
import { notify } from '../../../../yali-sdk/utils/NotifyRegistryCenter';
import { showFail, showSuccess } from '../../../config/common';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location: {},
    searchList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.newLocation();
  },
  onChange(e) {
    const value = e.detail.value;
    const qqmapsdk = new QQMapWX({
      key: 'PMFBZ-RANLG-C4OQT-IKVXS-PO3FQ-D5FEO' // 必填
    });
    qqmapsdk.search({
      keyword: value,
      success: (res) => {
        this.setData({ searchList: res.data });
      },
      fail: (res) => {
        showFail && showFail('搜索地址失败')
      },
    });
  },
  // 重新定位
  newLocation() {
    getLocation && getLocation((location) => {
      this.setData({ location });
    });
  },
  // 选择地点
  onSelect(e) {
    const item = getIn(e.currentTarget.dataset, ['item']); console.log(item)
    if (item) {
      getApp().globalData.longitude = getIn(item.location,['lng']);
      getApp().globalData.latitude = getIn(item.location,['lat']);
      getApp().globalData.address = item.title;
      notify && notify('updateLocation', item.title);
    } else {
      const location = wx.getStorageSync('location');
      getApp().globalData.longitude = location.longitude;
      getApp().globalData.latitude = location.latitude;
      getApp().globalData.address = location.address;
      notify && notify('updateLocation', location.address);
    }
    global.navigateHelper.navigateBack();
  }
})